import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
import store from '@/store/'

const request:AxiosInstance = axios.create({

})

request.interceptors.request.use((config:AxiosRequestConfig) => {
  console.log('configrequest', config)
  store.commit('addAxiosShowLoading')
  return config
}, (error:AxiosError) => {
  console.log('errorrequest', error)
  store.commit('errorAxiosShowLoading')
  return Promise.reject(error)
})

request.interceptors.response.use((config:AxiosResponse) => {
  console.log('configresponse', config)
  store.commit('removeAxiosShowLoading')
  return config
}, (error:AxiosError) => {
  console.log('errorresponse', error)
  store.commit('errorAxiosShowLoading')
  return Promise.reject(error)
})

export default <T>(config:AxiosRequestConfig):any => {
  return request(config).then(res => (res.data || res) as T)
}
